package vtt.android.docbaovn.fragments;

import java.util.List;

import vtt.android.docbaovn.Constants;
import vtt.android.docbaovn.adapters.RssAdapter;
import vtt.android.docbaovn.database.CategoryDAO;
import vtt.android.docbaovn.database.RssSourceDAO;
import vtt.android.docbaovn.entities.Category;
import vtt.android.rsslib.RssItem;
import android.os.Bundle;

public class FeedSumaryListFragment extends BaseNewFeedFragment {
    
    private CategoryDAO categoryDb;
    private RssSourceDAO rssDao;

    public static FeedSumaryListFragment newInstance(int sectionNumber) {
        FeedSumaryListFragment fragment = new FeedSumaryListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        categoryDb = new CategoryDAO(mParentAct);
        rssDao = new RssSourceDAO(mParentAct);
        categoryDb.open();
        rssDao.open();
        
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void updateListData(List<RssItem> result) {
        // only get 10 items every page
        for (int i = 0; i < Constants.NUM_PER_PAGE; i++) {
            if (result.size() > 0 && i < result.size()) {
                if (i == 0) {
                    RssItem adItem = new RssItem();
                    adItem.setTitle(RssAdapter.SHOW_ADMOB);
                    mItemList.add(adItem);
                }
                mItemList.add(result.get(i));
            } else {
                break;
            }
        }
    }

    /**
     * get Rss resource list from category
     */
    @Override
    public List<String> getRssResoureList(int tabIndex) {
        List<Category> categoryList = categoryDb.getCategory(true);
        Category category = categoryList.get(tabIndex);
        
        List<String> rssListString = rssDao.getRssListString(category.type);
        return rssListString;
    }

    @Override
    public boolean isShowHeader() {
        return true;
    }
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        categoryDb.close();
        rssDao.close();
    }
}
